/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.newsreader;

import java.util.ArrayList;
import java.util.List;

import com.example.android.newsreader.frameAux.NewsReaderActivityPreguntas;
import com.example.android.newsreader.previo.Preguntas;
import com.example.android.newsreader.previo.base.ManagerDB;
import com.example.android.newsreader.previo.clases.encuesta.Applicacion;
import com.example.android.newsreader.previo.clases.encuesta.Opcion;
import com.example.android.newsreader.previo.clases.encuesta.Pregunta;
import com.example.android.newsreader.previo.clases.encuesta.Seccion;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Fragment that displays a news article.
 */
public class ArticleFragment extends Fragment implements OnItemClickListener{
    // The webview where we display the article (our only view)
    WebView mWebView;
    ManagerDB manager;
    // The article we are to display
    Seccion mNewsArticle = null;

    // Parameterless constructor is needed by framework
    public ArticleFragment() {
        super();
    }

    TextView txt;
    ListView lis;
    String idApp;
    String idSecc;
    
    /**
     * Sets up the UI. It consists if a single WebView.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	idApp=getActivity().getIntent().getStringExtra("idApp");
    	idSecc=getActivity().getIntent().getStringExtra("idSecc");
		manager= new ManagerDB(getActivity());
		
		    	
    	
        View view =inflater.inflate(R.layout.encuesta, container, false);
        lis=(ListView)view.findViewById(R.id.listseccion);
        
                
        
       final List<Pregunta> preguntas =manager.getPreguntasBySeccion(idSecc,idApp);	
			
        
		
       ArrayAdapter<Pregunta> arr = new ArrayAdapter<Pregunta>(getActivity(),android.R.layout.simple_list_item_2,android.R.id.text1,preguntas){
			
            @Override
            public View getView(int position, View convertView, android.view.ViewGroup parent) {
            	
                View view = super.getView(position, convertView, parent);	 
                Pregunta entry = preguntas.get(position);
                TextView text1 = (TextView) view.findViewById(android.R.id.text1);
                text1.setTextColor(Color.BLACK);
                
                TextView text2 = (TextView) view.findViewById(android.R.id.text2);
                text1.setText("Pregunta " + (position + 1) + " :" + ((Integer.parseInt(entry.getTipoPregunta()) >= 4)?entry.getSubseccionInfo():entry.getTexto()));
                text2.setText((Integer.parseInt(entry.getTipoPregunta()) >= 4)?entry.getTexto():entry.getTextoAuxiliar());
                text2.setTextColor(Color.BLACK);
                
                
 if(entry.getRequerida().equals("1")){
					
					text1.setTextColor(Color.parseColor("#670202"));   
					text2.setTextColor(Color.parseColor("#670202"));   
				}
				
				if(entry.getRespuesta()!=null){
					text1.setTextColor(Color.parseColor("#01511a"));
					text2.setTextColor(Color.parseColor("#01511a"));
					
				}else{
					if(entry.getOpciones()!=null){
						for (Opcion opc : entry.getOpciones()) {
							if(opc.getRespuesta()!=null){
								text1.setTextColor(Color.parseColor("#01511a"));
								text2.setTextColor(Color.parseColor("#01511a")); 
	     					}
					}           						
					}     						
				}     			
                
                text1.setGravity(android.view.Gravity.CENTER);
                text2.setGravity(android.view.Gravity.CENTER);	                
                return view;
            }
        };
		
		
		lis.setAdapter(arr);      
        
        
        
        
    	return view;
    	
    }

    /**
     * Displays a particular article.
     *
     * @param article the article to display
     */
    public void displayArticle(String idSecc,String idApp) {
       this.idSecc=idSecc;
       this.idApp=idApp;
        loadWebView();
    }

    /**
     * Loads article data into the webview.
     *
     * This method is called internally to update the webview's contents to the appropriate
     * article's text.
     */
    void loadWebView() {
    
    	final List<Pregunta> preguntas =manager.getPreguntasBySeccion(idSecc,idApp);	
 		
ArrayAdapter<Pregunta> arr = new ArrayAdapter<Pregunta>(getActivity(),android.R.layout.simple_list_item_2,android.R.id.text1,preguntas){
			
            @Override
            public View getView(int position, View convertView, android.view.ViewGroup parent) {
            	
                View view = super.getView(position, convertView, parent);	 
                Pregunta entry = preguntas.get(position);
                TextView text1 = (TextView) view.findViewById(android.R.id.text1);
                text1.setTextColor(Color.BLACK);
                
                TextView text2 = (TextView) view.findViewById(android.R.id.text2);
                text1.setText("Pregunta " + (position + 1) + " : " + ((Integer.parseInt(entry.getTipoPregunta()) >= 4)?entry.getSubseccionInfo():entry.getTexto()));
                text2.setText((Integer.parseInt(entry.getTipoPregunta()) >= 4)?entry.getTexto():entry.getTextoAuxiliar());           
                text2.setTextColor(Color.BLACK);
                
                
                
                
                if(entry.getRequerida().equals("1")){
					
					text1.setTextColor(Color.parseColor("#670202"));   
					text2.setTextColor(Color.parseColor("#670202"));   
				}
				
				if(entry.getRespuesta()!=null){
					text1.setTextColor(Color.parseColor("#01511a"));
					text2.setTextColor(Color.parseColor("#01511a"));
					
				}else{
					if(entry.getOpciones()!=null){
						for (Opcion opc : entry.getOpciones()) {
							if(opc.getRespuesta()!=null){
								text1.setTextColor(Color.parseColor("#01511a"));
								text2.setTextColor(Color.parseColor("#01511a")); 
	     					}
					}           						
					}     						
				}     		
                
                
                text1.setGravity(android.view.Gravity.CENTER);
                text2.setGravity(android.view.Gravity.CENTER);	                
                return view;
            }
        };
		
		
		lis.setAdapter(arr); 
		lis.setOnItemClickListener(this);
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Pregunta preg = (Pregunta)lis.getAdapter().getItem(arg2);
		Intent intent=new Intent(getActivity(),NewsReaderActivityPreguntas.class);
		intent.putExtra("idApp", idApp);
		intent.putExtra("idSecc", idSecc);
		intent.putExtra("idPregunta",preg.getIdPregunta());
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		getActivity().startActivity(intent);
	//	getActivity().overridePendingTransition(R.anim.animation_leave,R.anim.animation_enter);
		
	}
}
